﻿using Dataforge.PrismAvalonExtensions;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.ServiceLocation;
using SampleApplication2.Constants;
using SampleApplication2.Views.Tools;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;

namespace SampleApplication2.Views.Ribbon
{
  public class HomeTabViewModel : NotificationObject
  {
    public HomeTabViewModel()
    {
      RegionManager.Regions[RegionNames.DockingRegion].Views.CollectionChanged += (sender, e) =>
        {
          switch (e.Action)
          {
            case NotifyCollectionChangedAction.Add:
              if (e.NewItems[0].Equals(RecentFilesView)) IsRecentFilesChecked = true;
              if (e.NewItems[0].Equals(FileStatsView)) IsFileStatsChecked = true;
              break;

            case NotifyCollectionChangedAction.Remove:
              if (e.OldItems[0].Equals(RecentFilesView)) IsRecentFilesChecked = false;
              if (e.OldItems[0].Equals(FileStatsView)) IsFileStatsChecked = false;
              break;
          }
        };
    }

    #region IRegionManager RegionManager

    IRegionManager RegionManager
    {
      get { return ServiceLocator.Current.GetInstance<IRegionManager>(); }
    }

    #endregion

    #region RecentFiles

    #region RecentFilesView

    RecentFilesView RecentFilesView
    {
      get { return (RecentFilesView)ServiceLocator.Current.GetInstance<IToolView>(RecentFilesViewModel.CONTENT_ID); }
    }

    #endregion

    #region ICommand ToggleRecentFilesCommand

    DelegateCommand _toggleRecentFilesCommand;
    public ICommand ToggleRecentFilesCommand
    {
      get { return _toggleRecentFilesCommand ?? (_toggleRecentFilesCommand = new DelegateCommand(OnToggleRecentFiles)); }
    }

    private void OnToggleRecentFiles()
    {
      IRegion dr = RegionManager.Regions[RegionNames.DockingRegion];
      if (dr.Views.Contains(RecentFilesView))
      {
        dr.Remove(RecentFilesView);
      }
      else
      {
        if (dr.Views.Contains(FileStatsView))
        {
          dr.Add(new DockingMetadata(RecentFilesView, new NestedDockStrategy(FileStatsView, NestedDockPosition.Inside)));
        }        
        else
        {
          dr.Add(new DockingMetadata(RecentFilesView, new SideDockStrategy(DockSide.Bottom, new GridLength(150))));
        }
      }
    }

    #endregion

    #region bool IsRecentFilesChecked

    bool _isRecentFilesChecked;
    public bool IsRecentFilesChecked
    {
      get { return _isRecentFilesChecked; }
      set
      {
        _isRecentFilesChecked = value;
        RaisePropertyChanged("IsRecentFilesChecked");
      }
    }

    #endregion

    #endregion

    #region FileStats

    #region FileStatsView

    FileStatsView FileStatsView
    {
      get { return (FileStatsView)ServiceLocator.Current.GetInstance<IToolView>(FileStatsViewModel.CONTENT_ID); }
    }

    #endregion

    #region ICommand ToggleFileStatsCommand

    DelegateCommand _toggleFileStatsCommand;
    public ICommand ToggleFileStatsCommand
    {
      get { return _toggleFileStatsCommand ?? (_toggleFileStatsCommand = new DelegateCommand(OnToggleFileStats)); }
    }

    private void OnToggleFileStats()
    {
      IRegion dr = RegionManager.Regions[RegionNames.DockingRegion];
      if (dr.Views.Contains(FileStatsView))
      {
        dr.Remove(FileStatsView);
      }
      else
      {
        if (dr.Views.Contains(RecentFilesView))
        {
          dr.Add(new DockingMetadata(FileStatsView, new NestedDockStrategy(RecentFilesView, NestedDockPosition.Inside)));
        }
        else
        {
          dr.Add(new DockingMetadata(FileStatsView, new SideDockStrategy(DockSide.Bottom, new GridLength(150))));
        }
      }
    }

    #endregion

    #region bool IsFileStatsChecked

    bool _isFileStatsChecked;
    public bool IsFileStatsChecked
    {
      get { return _isFileStatsChecked; }
      set
      {
        _isFileStatsChecked = value;
        RaisePropertyChanged("IsFileStatsChecked");
      }
    }

    #endregion

    #endregion
  }
}
